package com.wshl.aijiuunion.utils;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wshl.aijiuunion.GlobConstant;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.controller.fragment.CenterFragment;
import com.wshl.aijiuunion.controller.fragment.HomeFragment;
import com.wshl.aijiuunion.controller.fragment.MineFragment;
import com.wshl.aijiuunion.controller.fragment.ShopFragment;
import com.wshl.aijiuunion.controller.fragment.VideoFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created buy 风凊扬 on 2019/10/23 0023
 */
public class HomeActivity2 extends AppCompatActivity {

    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.tabHost)
    MyFragmentTabHost tabHost;
    @BindView(R.id.listen)
    ImageView listen;

    private String currentpage = "首页";
    //五个按钮的写法
    private String[] tabs = new String[]{"首页", "商城", "center", "视频", "我的"};
    private Class[] mFragmentClasses = new Class[]{HomeFragment.class, ShopFragment.class, CenterFragment.class,
            VideoFragment.class, MineFragment.class};
    private int[] selectorImg = new int[]{R.drawable.bottom_home_selector, R.drawable.bottom_shop_selector, R.drawable.ic_launcher_foreground,
            R.drawable.bottom_video_selector, R.drawable.bottom_mine_selector};


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        listen.setVisibility(View.VISIBLE);
        // 初始化tabHost
        tabHost.setup(this, getSupportFragmentManager(), R.id.fl_content);
        tabHost.getTabWidget().setShowDividers(0);//去掉分割线
        for (int i = 0; i < tabs.length; i++) {
            tabHost.addTab(tabHost.newTabSpec(tabs[i]).setIndicator(getTabView(i)), mFragmentClasses[i], null);
        }
        // 设置默认tab
        tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                currentpage = tabId;
                if (tabId.equals("center")) {
                    listen.setSelected(true);
                } else {
                    listen.setSelected(false);
                }

            }
        });
    }


    private View getTabView(int index) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.tab_indicator, null);
        TextView tv_indicator = (TextView) inflate.findViewById(R.id.tv_indicator);

        //中间带按钮的写法
        if (index == 2) {
            tv_indicator.setVisibility(View.INVISIBLE);
        } else {
            Drawable drawable = getApplicationContext().getResources().getDrawable(selectorImg[index]);
            tv_indicator.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
            tv_indicator.setText(tabs[index]);

        }

        return inflate;
    }

    @OnClick(R.id.listen)
    public void onViewClicked() {

        tabHost.setCurrentTab(2);
    }


    private long mExitTime;// 上一次点击返回键的时间

    // 重写onKeyDown方法
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {  // 是返回键

            LogUtils.e(currentpage);
            if (currentpage.equals("首页")) {

                //如果当前显示的fragment是首页
                // 点击两次返回键退出程序
                if (System.currentTimeMillis() - mExitTime > 2000) {
                    // 如果当前点击返回键的时间减去上一次点击返回键的时间大于2秒
                    ToastUtils.showShort("再按一次退出");
                    // 重新赋值,方便下次使用
                    mExitTime = System.currentTimeMillis();
                } else {
                    // 如果小于两秒,就直接退出程序
                    SPUtils.getInstance().put(GlobConstant.ISSTARTMIAN, true);
                    finish();

                }
            } else {
                //如果当前不是首页,先切换到首页
                tabHost.setCurrentTab(0);
            }


            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
